package com.baitengsoft.patentapimonitor.api.service.impl;

import com.baitengsoft.patentapimonitor.api.dao.WebMenuDao;
import com.baitengsoft.patentapimonitor.api.entity.WebMenuEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebUsermenuEntity;
import com.baitengsoft.patentapimonitor.api.model.WebUserMenuModel;
import com.baitengsoft.patentapimonitor.api.service.WebUserMenuService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by QEK on 2017/12/25.
 */
@Service
public class WebUserMenuServiceImpl extends BaseServiceImpl<WebUsermenuEntity> implements WebUserMenuService {

    @Autowired
    private WebMenuDao webMenuDao;
    @Resource
    Mapper mapper;


    /**
     * 根据userId查询用户菜单
     * @param userId
     * @return
     */
    @Override
    public List<WebUserMenuModel> getUserMenuListByUserId(Integer userId) {
        List<WebUserMenuModel> models = new ArrayList<>();
        //查询一级菜单
        List<WebMenuEntity> oneLevelList = webMenuDao.selectOneLevelMenuByUserId(userId);
        if(oneLevelList == null || oneLevelList.size() == 0 ) return null;
        for(WebMenuEntity oneLevelEntity : oneLevelList){
            WebUserMenuModel model = new WebUserMenuModel();
            //查询一级下的二级
            List<WebMenuEntity> twoLevelList = webMenuDao.selectTwoLevelMenuByUserIdAndParentId(userId,oneLevelEntity.getId());
            mapper.map(oneLevelEntity,model);
            model.setTwolevelmenulist(twoLevelList);
            models.add(model);
        }
        return models;

    }

    @Override
    public List<WebUsermenuEntity> searchByUserId(int userId) {

        WebUsermenuEntity entity=new WebUsermenuEntity();
        entity.setUserid(userId);
        List<WebUsermenuEntity> entities=queryListByWhere(entity);
        return entities;
    }

    @Override
    public Integer delUsermenuByUserId(int userId) {

        WebUsermenuEntity entity=new WebUsermenuEntity();
        entity.setUserid(userId);
        Integer successId=deleteByWhere(entity);

        return successId;
    }

    @Override
    public Integer saveUsermenu(WebUsermenuEntity entity) {

        Integer successId= saveByNotNull(entity);
        return successId;
    }
}
